package com.offcn.member.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.auth0.jwt.JWT;
import com.offcn.common.utils.JWTUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import com.offcn.member.entity.MemberEntity;
import com.offcn.member.service.MemberService;
import com.offcn.common.utils.PageUtils;
import com.offcn.common.utils.R;

import javax.annotation.Resource;


/**
 * 会员-会员表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-07-21 22:56:43
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
     @RequestMapping("/countMember")
    public R countMember(String beginTime,String endTime){
        List<Map<String, Object>> mapList = memberService.countMember(beginTime, endTime);
       return R.ok().put("mapList",mapList);
    }
   @Resource
   private RedisTemplate redisTemplate;
    @PostMapping("/login/{username}/{password}")
    public R login(@PathVariable String username,@PathVariable String password){
        MemberEntity memberEntity = memberService.login(username, password);
        if (null!=memberEntity){
            //获取token将其存入Redis里
            String token = JWTUtil.generateToken(memberEntity.getUserName());
            //生成refreshToken
            String refreshToken = UUID.randomUUID().toString().replace("-", "");
            redisTemplate.opsForHash().put(refreshToken,"token",token);
            redisTemplate.opsForHash().put(refreshToken,"username",username);
            //设置token的过期时间
            redisTemplate.expire(refreshToken,JWTUtil.REFRESH_TOKEN_EXPIRE_TIME, TimeUnit.MILLISECONDS);
            return R.ok().put("token",token).put("refreshToken",refreshToken);
        }else {
            return R.error("账号或密码错误");
        }
    }
     //刷新refreshtoken
    @PostMapping("/refreshtoken")
    public R refreshToken(String refreshToken){
        //通过refreshToken获取用户名
      String username = (String) redisTemplate.boundHashOps(refreshToken).get("username");
      if (StringUtils.isEmpty(username)){
          return R.error("刷新失败");
      }
        //根据用户名生成新的token
        String token = JWTUtil.generateToken(username);
      //存入Redis
      redisTemplate.boundHashOps(refreshToken).put("token",token);
      return R.ok().put("token",token).put("refreshToken",refreshToken);
    }

}
